package org.dromara.third.convert.enterprise.validator;

import cn.dev33.satoken.annotation.SaIgnore;
import org.dromara.common.core.service.DeptService;
import org.dromara.common.core.service.UserService;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.convert.enterprise.dto.DeviceDTO;
import org.dromara.third.convert.riskObject.dto.RiskObjectDTO;
import org.dromara.third.convert.util.ExistenceValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.List;

@Component
@SaIgnore
public class DeviceValidator implements Validator {

    @Autowired
    private DeptService deptService;

    @Autowired
    private UserService userService;

    @Override
    public boolean supports(Class<?> clazz) {
        return List.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        List<DeviceDTO> deviceDTOS = (List<DeviceDTO>) target;

        // 基础字段验证
        validateRequiredFields(deviceDTOS, errors);

        validateExistence(deviceDTOS, errors);
    }

    /**
     * 验证必填字段
     */
    private void validateRequiredFields(List<DeviceDTO> dtos, Errors errors) {
        dtos.forEach(dto -> {
            ValidateUtil.rejectIfNull(dto::getId, String.valueOf(ExceptionEnum.AuthException01.getCode()), "主键不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSczzmc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "生产装置名称不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSczzbh, String.valueOf(ExceptionEnum.AuthException01.getCode()), "生产装置编号不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSsbm, String.valueOf(ExceptionEnum.AuthException01.getCode()), "所属部门不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getTysj, String.valueOf(ExceptionEnum.AuthException01.getCode()), "投用时间不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getFzr, String.valueOf(ExceptionEnum.AuthException01.getCode()), "负责人不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getLxfs, String.valueOf(ExceptionEnum.AuthException01.getCode()), "联系方式不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getZzyt, String.valueOf(ExceptionEnum.AuthException01.getCode()), "主要用途不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getZzzt, String.valueOf(ExceptionEnum.AuthException01.getCode()), "装置状态不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getDelFlag, String.valueOf(ExceptionEnum.AuthException01.getCode()), "删除标志不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getCreateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建时间不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getUpdateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新时间不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getCreateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建人不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getUpdateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新人不能为空", errors);
        });
    }

    private void validateExistence(List<DeviceDTO> dtos, Errors errors) {
        dtos.forEach(dto -> {
            ExistenceValidator.validateDeptExists(dto.getSsbm(), "生产装置所属部门", errors);
        });
    }
}
